package com.ryan.security.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.ryan.security.dto.User;
import com.ryan.security.dto.UserQueryCondition;
import com.ryan.security.exception.UserNotExistException;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kaimin on 31/12/2018.
 * time : 12:32
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/me")
    public Object getCurrentuser(@AuthenticationPrincipal UserDetails userDetails){
//        return SecurityContextHolder.getContext().getAuthentication();
        return userDetails;
    }

    @PutMapping("/{id:\\d+}")
    public User modify(@RequestBody @Valid User user, BindingResult errs){
        if (errs.hasErrors()) {
            // BindingResult和@Valid配合使用，如果没家BindingResult，就不会进该防范
            //但是现在又想进该方法，并且记录，则可以用BindingResult
            errs.getAllErrors().stream().forEach(err->{
                System.out.println(err.getDefaultMessage());
            });
        }
//        System.out.println("user:"+ ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println("id="+user.getId());
        System.out.println("name="+user.getUsername());
        System.out.println("pwd="+user.getPassword());
        System.out.println("data="+user.getBirthday());
        user.setId(1);
        return user;
    }

    @PostMapping
    public User create(@RequestBody @Valid User user, BindingResult errs){
        if (errs.hasErrors()) {
            // BindingResult和@Valid配合使用，如果没家BindingResult，就不会进该防范
            //但是现在又想进该方法，并且记录，则可以用BindingResult
            errs.getAllErrors().stream().forEach(err->{
                System.out.println(err.getDefaultMessage());
            });
        }
//        System.out.println("user:"+ ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println("id="+user.getId());
        System.out.println("name="+user.getUsername());
        System.out.println("pwd="+user.getPassword());
        System.out.println("data="+user.getBirthday());
        user.setId(1);
        return user;
    }

    @GetMapping
//    @RequestMapping(value = "/user",method = RequestMethod.GET)
//    public List<User> get(@RequestParam String username){
    @JsonView(User.UserSimpleView.class)
    public List<User> get(UserQueryCondition condition){
        System.out.println("name:"+condition.getName());
        List users=new ArrayList();
//        users.add(new User("aaa","123",1));
//        users.add(new User("aaa","123",1));
//        users.add(new User("aaa","123",1));
        return users;
    }

//    @GetMapping("/user/{id}")//常规写法
    @GetMapping("/{id:\\d+}") //可以写表达式，只规定数字
    @JsonView(User.UserDetailView.class)
    public User getInfo(@PathVariable("id") String id){
        System.out.println("id="+id);
        return new User("aaa","123",1,new Date());
//        throw new UserNotExistException(id);
//        return null;
    }




}
